package sjd.ahsj.springbootmybatisplusoracle.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import sjd.ahsj.springbootmybatisplusoracle.entity.IDataDefine;
import sjd.ahsj.springbootmybatisplusoracle.entity.IFactory;
import sjd.ahsj.springbootmybatisplusoracle.mapper.IFactoryMapper;
import sjd.ahsj.springbootmybatisplusoracle.service.IIFactoryService;
import sjd.ahsj.springbootmybatisplusoracle.util.DateUtil;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhanghaogao
 * @since 2018-10-18
 */
@Service
public class IFactoryServiceImpl extends ServiceImpl<IFactoryMapper, IFactory> implements IIFactoryService {


    /**
     * 创建实时和历史表
     *
     * @param iDataDefines
     */
    @Override
    public Integer createCurrentAndHistory(Long factoryId, List<IDataDefine> iDataDefines) {
        String tableName = "i_current_";
        if (factoryId < 10) {
            tableName = "i_current_0";
        }
        String table1 = tableName + factoryId + "_1";
        String table2 = tableName + factoryId + "_2";
        String haSql = "";
        String sql1 = "";
        String sql2 = "";
        String sql3 = "";
        for (IDataDefine info : iDataDefines) {
            String type = "varchar(50)";
            if (info.getDataType().equals("int")) {
                type = "integer(11)";
            }
            if (info.getDataType().equals("float")) {
                type = "float(11)";
            }

            if (info.getHistory() == 0) {
                if (info.getKey().indexOf("PA") >= 0) {
                    sql3 = sql3 + " `" + info.getKey() + "` " + type + " NULL, ";
                }
                if (Integer.parseInt(info.getKey().substring(4, 6)) > 5) {
                    sql2 = sql2 + " `" + info.getKey() + "` " + type + " NULL, ";
                } else {
                    sql1 = sql1 + " `" + info.getKey() + "` " + type + " NULL, ";
                }

            }
            if (info.getHistory() == 1) {
                haSql = haSql + " `" + info.getKey() + "` " + type + " NULL, ";
            }


        }
        baseMapper.createCurrent(table1, sql1);
        baseMapper.createCurrent(table2, sql2);
        //新建历史表
        tableName = "i_history_";
        if (factoryId < 10) {
            tableName = "i_history_0";
        }
        tableName = tableName + factoryId;
        baseMapper.createHistory(tableName, haSql);
        //新建会图表
        tableName = "i_plot_";
        if (factoryId < 10) {
            tableName = "i_plot_0";
        }
        tableName = tableName + factoryId;
        sql3 = sql3 + " `type` integer(11) NULL ";
        baseMapper.createHistory(tableName, sql3);
        return 1;
    }

    /**
     * 获取电站历史数据
     *
     * @param factoryId
     * @return
     */
    @Override
    public List<Map<String, Object>> selectFactoryInfoHistory(Integer factoryId, Date date) {
        String tableName = "i_history_";
        if (factoryId < 10) {
            tableName = "i_history_0";
        }
        String table = tableName + factoryId;
        return baseMapper.selectFactoryInfoHistory(table, DateUtil.getStartTime(date), DateUtil.getEndTime(date));
    }

    /**
     * 获取机组信息
     *
     * @param num
     * @return
     */
    @Override
    public Map<String, Object> selectFactoryInfo(Integer num, Integer factoryId) {
        String tableName = "i_current_";
        if (factoryId < 10) {
            tableName = "i_current_0";
        }
        String table = tableName + factoryId + "_1";
        Map<String, Object> map = baseMapper.selectFactoryInfo(table).get(0);
        table = tableName + factoryId + "_2";
        map.putAll(baseMapper.selectFactoryInfo(table).get(0));
        return map;
    }

    /**
     * 获取机组信息
     *
     * @param num
     * @param factoryId
     * @return
     */
    @Override
    public Map<String, Object> selectFactoryInfo(Integer num, Integer factoryId, String sql) {
        String tableName = "i_current_";
        if (factoryId < 10) {
            tableName = "i_current_0";
        }
        String table = tableName + factoryId + "_" + num;
        return baseMapper.selectFactoryInfoBySql(table, sql).get(0);
    }

    /**
     * 新增历史数据
     *
     * @param map
     * @return
     */
    @Override
    public Integer insertHistoryInfo(Integer factoryId, Map<String, Object> map) {
        String tableName = "i_history_";
        if (factoryId < 10) {
            tableName = "i_history_0";
        }
        tableName = tableName + factoryId;
        String values = "";
        String keys = "";
        Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, Object> entry = entries.next();
            keys = keys + entry.getKey() + ",";
            values = values + "'" + entry.getValue() + "',";
        }

        values = values + "'" + DateUtil.dateFormatToString(new Date(), "yyyy-MM-dd HH") + "'";
        keys = keys + "create_date";
        return baseMapper.insertHistoryInfo(tableName, keys, values);
    }

    /**
     * 新增绘图数据
     *
     * @param factoryId
     * @param map
     * @return
     */
    @Override
    public Integer insertPlotInfo(Integer factoryId, Map<String, Object> map, Date date) {
        String tableName = "i_plot_";
        if (factoryId < 10) {
            tableName = "i_plot_0";
        }
        tableName = tableName + factoryId;
        String values = "";
        String keys = "";
        Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, Object> entry = entries.next();
            keys = keys + entry.getKey() + ",";
            values = values + "'" + entry.getValue() + "',";
        }

        values = values + "'" + DateUtil.dateFormatToString(date, "yyyy-MM-dd HH:mm:ss") + "'";
        keys = keys + "create_date";
        return baseMapper.insertHistoryInfo(tableName, keys, values);
    }

    /**
     * 查询绘图表平均值
     *
     * @param factoryId
     * @param sql
     * @param stDate
     * @param enDate
     * @param type
     * @return
     */
    @Override
    public Map<String, Object> selectPlotInfoBySql(Integer factoryId, String sql, Date stDate, Date enDate,
                                                   Integer type) {
        String tableName = "i_plot_";
        if (factoryId < 10) {
            tableName = "i_plot_0";
        }
        tableName = tableName + factoryId;
        return baseMapper.selectPlotInfoBySql(tableName, sql, stDate, enDate, type).get(0);
    }


    @Override
    public List<Map<String, Object>> selectPlotInfoBySql(Integer factoryId, Integer type) {
        String tableName = "i_plot_";
        if (factoryId < 10) {
            tableName = "i_plot_0";
        }
        tableName = tableName + factoryId;
        return baseMapper.selectPlotInfoByType(tableName, type);
    }

    /**
     * 删除绘图表历史数据
     *
     * @param factoryId
     * @param enDate
     * @param type
     * @return
     */
    @Override
    public Integer delPlotInfoByType(Integer factoryId, Date enDate, Integer type) {
        String tableName = "i_plot_";
        if (factoryId < 10) {
            tableName = "i_plot_0";
        }
        tableName = tableName + factoryId;
        return baseMapper.delPlotInfoByType(tableName, enDate, type);
    }
}
